package com.freda.oa.flow.support.impl;

import javax.servlet.http.HttpServletRequest;

import org.eredlab.g4.bmf.base.BaseServiceImpl;

import com.freda.oa.flow.run.service.impl.FlowQueryServiceImpl;
import com.freda.oa.flow.run.service.itf.FlowRunServiceItf;
import com.freda.oa.flow.run.vo.FlowCommonListenerVo;
import com.freda.oa.flow.run.vo.FlowRunInfoVo;
import com.freda.oa.flow.support.Itf.FlowListenerSupportItf;
import com.freda.oa.flow.support.Itf.FlowRunSupport;

public class FlowRunSupportImpl extends BaseServiceImpl implements FlowRunSupport{
	
	private FlowRunServiceItf manager ;
	
	private  FlowListenerSupportItf listener ;
	
	private FlowRunInfoVo runInfo ;
	
	
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#startFlow(com.freda.oa.flow.run.vo.FlowRunInfoVo, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public synchronized int startFlow(HttpServletRequest request)
	{
		
		runInfo.setRequest(request);
		
		FlowCommonListenerVo fl = this.listener.CreateAllListeners(runInfo, "before");
		
		if(listener.excuteListeners(runInfo, fl, "before") == 1)
		{
			
		}
		
		return 1;
	}
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#next(com.freda.oa.flow.run.vo.FlowRunInfoVo, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public synchronized int next(HttpServletRequest request)
	{
		
		this.runInfo.setRequest(request);
		
		FlowCommonListenerVo fl = this.listener.CreateAllListeners(runInfo, "before,completed");
		
		if(listener.excuteListeners(runInfo, fl, "before") != 1){
			return 0;			
		}
		
		if(runInfo.getNextActivityId() != null && !runInfo.getNextActivityId().equals(""))
		{
			if(runInfo.getNextActivityType().equals("task"))
			{
				manager.silmpleToNext(runInfo);
			}
			else if(runInfo.getNextActivityType().equals("foreach"))
			{
				manager.foreachToNext(runInfo);
			}
			else if(runInfo.getNextActivityType().equals("join"))
			{
				manager.joinToNext(runInfo);
			}
			else if(runInfo.getNextActivityType().equals("state"))
			{
				manager.stateToNext(runInfo);
			}
			else if(runInfo.getNextActivityType().equals("perusal"))
			{
				manager.doPerusal(runInfo);
			}
			else if(runInfo.getNextActivityType().equals("end"))
			{
				manager.flowToEnd(runInfo);
			}
			else
			{
				return 0;
			}
			
		}
		else
		{
			if(runInfo.isPerusalCompleted())
			{
				manager.perusalCompleted(runInfo);
			}
			else
			{
				return 0;				
			}
		}
		
		if(listener.excuteListeners(runInfo, fl, "completed") != 1)
		{
			return 0;
		}
		
		return 1;
	}
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#open(com.freda.oa.flow.run.vo.FlowRunInfoVo, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public FlowRunInfoVo open(HttpServletRequest request)
	{
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#lockAssignee(com.freda.oa.flow.run.vo.FlowRunInfoVo)
	 */
	@Override
	public synchronized int lockAssignee()
	{
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#unLockAssignee(com.freda.oa.flow.run.vo.FlowRunInfoVo)
	 */
	@Override
	public synchronized int unLockAssignee()
	{
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowRunSupportItf#getFlowInfo(com.freda.oa.flow.run.vo.FlowRunInfoVo)
	 */
	@Override
	public FlowRunInfoVo getFlowInfo(String type)
	{
		return runInfo;
	}

	/**
	 * @param manager the manager to set
	 */
	public void setManager(FlowRunServiceItf manager) {
		this.manager = manager;
	}

	/**
	 * @param listener the listener to set
	 */
	public void setListener(FlowListenerSupportItf listener) {
		this.listener = listener;
	}

	/**
	 * @param runInfo the runInfo to set
	 */
	public void setRunInfo(FlowRunInfoVo runInfo) {
		this.runInfo = runInfo;
	}
	
	

}
